<template>
  <div class="bgcor" @touchmove="touchmove" ref="boxHeight">

    <BomNav :active="active"></BomNav>

    <div>
      <!-- 搜索框 -->
      <div class="serach">
        <div>
          <van-search v-model="value" show-action label="" placeholder="请输入搜索关键词" @search="onSearch">
            <template #action>
              <div class="searchsss" @click="onSearch">搜索</div>
            </template>
          </van-search>
        </div>
      </div>
      <!-- 轮播图 -->
      <div v-if="bannerData.datas?.length > 0">
        <van-swipe class="my-swipe" :autoplay="2000" indicator-color="white">
          <div v-for="item, i in bannerData.datas" :key="item.id">
            <van-swipe-item v-if="i < 10">
              <img @click="seleteListData(item)" :src="item.url" alt="">
              <span>
                {{ item.title }}
              </span>
            </van-swipe-item>
          </div>
        </van-swipe>
      </div>
      <div style="height: 15rem;">

      </div>
      <!-- 手机专区 -->
      <div class="phonezhuanqu">
        <div>
          手机专区
        </div>
        <div>
          <div v-for="item in phonezhuanqu.datas" :key="item.id">
            <div>
              <img @click="seleteListData(item)" :src="item.url" alt="">
            </div>
            <div>
              <van-text-ellipsis :content="item.title" />

            </div>
            <div>
              ￥ <span> {{ item.price }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="phonezhuanqu">
        <div>
          耳机
        </div>
        <div>
          <div v-for="item in erji.datas" :key="item.id">
            <div>
              <img @click="seleteListData(item)" :src="item.url" alt="">
            </div>
            <div>
              <van-text-ellipsis :content="item.title" />

            </div>
            <div>
              ￥ <span> {{ item.price }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="tuijians">为你推荐</div>
      <div class="tuijian">
        <div v-for="item in homeDatas.datas" :key="item.id">
          <div>
            <img @click="seleteListData(item)" :src="item.url" alt="">
          </div>
          <div>
            <div @click="seleteListData(item)">
              {{ item.title }}
            </div>
            <div @click="seleteListData(item)">
              {{ item.secondTitle }}
            </div>
            <div>
              <div>
                <span> ￥</span> <span>{{ item.price }}</span>
              </div>
              <div @click="addgouwuche(item.id)">
        <van-icon name="cart-o" />
              
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="tianchong">
    </div>
  </div>
  <van-back-top right="15vw" bottom="10vh" />
</template>

<script lang="ts" setup>
import BomNav from "@/components/BomNav.vue";
import { ref, reactive } from "vue";
import { listData, addgouwuches, rdomlistDataAll, liketypenames } from '@/api/index'
import { showToast } from "vant";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
let store = useStore()
let active = ref(0)
let value = ref('')
let router = useRouter()
let phonezhuanqu = reactive({
  datas: [] as any
})
const onSearch = () => {
  liketypenames(value.value, value.value, 0, 20).then((result) => {
    if (result.data.msg == '没有该数据') {
      showToast('没有相关数据哦')
    } else {
      store.commit('updateSearchdata', result.data.data)
      router.push({
        path: '/search'
      })
    }
  }).catch((err) => {
    console.log(err);
  });
};

liketypenames('手机', '', 0, 9).then((result: any) => {
  phonezhuanqu.datas = result.data.data

}).catch((err: any) => {
  console.log(err);
});

let erji = reactive({
  datas: [] as any
})
liketypenames('耳机', '耳机', 0, 9).then((result: any) => {
  erji.datas = result.data.data

}).catch((err: any) => {
  console.log(err);
});


let page = reactive({
  page: 0,
  pagesize: 20
})
//轮播图
let bannerData = reactive({
  datas: [] as any
})
rdomlistDataAll(6).then((result) => {
  bannerData.datas = result.data.data

}).catch((err) => {
});

let seleteListData = (id: Number) => {
  router.push({
    path: `/xiangqing?id=${id}`
  })
  store.commit('updataxiangqingdata', id)
}
let addgouwuche = (id: Number) => {

  addgouwuches(id, store.state.userData[0].id).then((result: any) => {

    showToast(result.data.msg)
  }).catch((err: any) => {
    showToast('网络错误')
  });
}

let homeDatas = reactive({
  datas: [] as any
})
rdomlistDataAll(20).then((result) => {
  result.data.data.forEach((item: any) => {
    homeDatas.datas.push(item)
  })
}).catch((err) => {
});

let boxHeight: any = ref(null)
let nums = 0
let touchmove = (e: any) => {
  if (e.touches[0].pageY > boxHeight.value.clientHeight - 500) {
    nums++
  }
  if (nums == 1) {
    showToast({
      message: '正在加载中。。。。。。',
      position: 'bottom',
    });
    rdomlistDataAll(20).then((result) => {
      result.data.data.forEach((item: any) => {
        homeDatas.datas.push(item)
      })
      setTimeout(() => {

        nums = 0
      }, 800);
    }).catch((err) => {
    });
  }
}
</script>
<style lang="less" >
.van-back-top{
    background-color: #f83d3d;
}

.phonezhuanqu {
  text-align: left;
  width: 345rem;
  margin: 0 auto;

  >div:nth-child(1) {
    font-size: 16rem;
    font-weight: 600;
    margin-left: 20rem;

  }

  >div:nth-child(2) {

    margin-top: 15rem;
    margin-bottom: 20rem;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    >div {
      margin-top: 10rem;
      border-radius: 5rem;
      background-color: #f4fdfd;
      width: 110rem;

      >div:nth-child(2) {
        width: 80rem;
        margin: 0 auto;
      }

      >div:nth-child(3) {
        width: 80rem;
        margin: 0 auto;
        color: #f83d3d;
        font-size: 12rem;

        span {
          font-size: 14rem;
          font-weight: 600;
        }
      }

    }

    img {
      width: 110rem;
    }
  }
}

.van-swipe__indicator {
  background-color: #0a0a0a !important;
}

.tuijian {
  margin: 0 auto;
  border-left: 15rem solid #f4fdfd;
  border-right: 15rem solid #f4fdfd;

  >div {

    display: flex;
    flex-wrap: nowrap;
    justify-content: space-around;
    text-align: left;
    position: relative;
    top: 0;
    left: 0;

    >div:nth-child(1) {
      img {
        width: 150rem;
        height: 150rem;
        background-color: #fff;
        border-radius: 8rem;
      }
    }

    >div:nth-child(2) {
      border-bottom: 1px solid #f5f5f5;
      background-color: #fff;

      width: 170rem;

      >div:nth-child(1) {
        margin-top: 20rem;
        font-size: 16rem;
      }

      >div:nth-child(3) {
        position: absolute;
        bottom: 15rem;
        width: 170rem;
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: space-between;

        span:nth-child(1) {
          font-size: 14rem;
          color: #f83d3d;
          font-weight: 600;
        }

        span:nth-child(2) {
          font-size: 16rem;
          color: #f83d3d;
        }

        >div:nth-child(2) {
          text-align: center;
          width: 35rem;
          height: 35rem;
          line-height: 50rem;
          border-radius: 50%;
          text-align: center;
          color: #fff;
          background-color: rgb(243, 94, 94);

        }
        i{
          font-size: 25rem;
        }
      }
    }

  }

}

.tuijians {
  background-color: #f4fdfd;
  font-size: 24rem;
  padding-top: 15rem;
  padding-bottom: 8rem;
  color: #000000;
}

.tianchong {
  height: 50rem;
}
.van-back-top{
    background-color: #f83d3d;
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20rem;
  line-height: 150rem;
  text-align: center;
  background-color: #e5eef3;
  height: 170rem;
  overflow: hidden;
  width: 335rem !important;
  margin-left: 20rem !important;
  margin-right: 20rem !important;
  border-radius: 20rem;
  img {
    height: 150rem;
  }

  span {
    position: relative;
    top: -20rem;
    color: #585555;
  }
}

.serach {
  width: 355rem;
  margin: 0 auto;

  >div {
    width: 348rem;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    font-size: 14rem;

    .van-search {
      width: 380rem;
      font-size: 14rem;
      
    }

    span {
      display: 50rem;
      font-size: 14rem;
      font-weight: 600;

    }
  }
}
.bgcor{
  max-width: 100vw;
  overflow: hidden;
}

</style>
